package com.xinwei.leetcode.二叉树;
// https://leetcode-cn.com/problems/search-in-a-binary-search-tree/
public class _700_二叉搜索树中的搜索 {
    // 迭代
    public TreeNode searchBST(TreeNode root, int val) {
        TreeNode node = root;
        while (node != null) {
            if (node.val > val) {
                node = node.left;
            }else if (node.val < val) {
                node = node.right;
            }else if (node.val == val) {
                break;
            }
        }
        return node;
    }

    // 递归
    public TreeNode searchBST1(TreeNode root, int val) {
        if (root == null) {
            return root;
        }
        if (root.val == val) {
            return root;
        }
        return searchBST1((root.val > val) ? root.left : root.right, val);
    }
}
